package cn.kgc.junketing.provider.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisTemplateConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        RedisSerializer<?> stringSerializer = new StringRedisSerializer();
        RedisSerializer<?> genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        //  key序列化
        redisTemplate.setKeySerializer(stringSerializer);
        //  value序列化
        redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);
        //  hash key序列化
        redisTemplate.setHashKeySerializer(stringSerializer);
        //  hash value序列化
        redisTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer);
        //  设置连接工厂
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return redisTemplate;
    }
}
